import java.util.Iterator;

public class LinkQueue {
    public static void main(String[] args) {
        LinkedListQueue<Integer> queue = new LinkedListQueue<>(5);
        queue.offer(1);
        queue.offer(2);
        queue.offer(6);
        queue.offer(7);
        queue.offer(9);
        queue.offer(13);
        queue.offer(16);
        queue.offer(19);

        System.out.println( queue.isFull());

        for (Integer i: queue){
            System.out.println(i);

        }

        System.out.println( queue.isEmpty());
    }
}

class  LinkedListQueue<E> implements Queue<E>,Iterable<E>{



    public static class Node<E>{
        E value;
        Node<E> next;

        public Node(E value, Node<E> next){
            this.value = value;
            this.next = next;
        }
    }

    Node<E> head = new Node<>(null,null);
    Node<E> tail = head;
    int size = 0;
    int captive = Integer.MIN_VALUE;


    public LinkedListQueue(){
        tail.next = head;
    }

    public LinkedListQueue(int captive){
        this.captive = captive;
    }

    @Override
    public boolean offer(E value) {
        if (isFull()){
            return false;
        }
        Node<E> add= new Node<>(value,head);
        tail.next = add;
        tail = add;
        size++;
        return true;
    }

    @Override
    public E poll() {
        if (isEmpty()) {
            return null;
        }
        Node<E> remove = head.next;
        head.next = remove.next;
        if (remove == tail){
            tail = head;
        }
        size--;
        return remove.value;
    }

    @Override
    public E peek() {
        if (isEmpty()) {
            return null;
        }
        return head.next.value;
    }

    @Override
    public boolean isEmpty() {
        return head==tail;
    }

    @Override
    public boolean isFull() {
        return size==captive;
    }

    @Override
    public Iterator iterator() {
        return new Iterator() {
            Node<E> p = head.next;
            @Override
            public boolean hasNext() {
                return p!=head;
            }

            @Override
            public Object next() {
                E value = p.value;
                p = p.next;
                return value;
            }
        };
    }
}
